package modulesAPI.v2.composite;

import java.util.ArrayList;

import warehouse.Resource;
import modulesAPI.v2.Module;
import modulesAPI.v2.ViewType;
import modulesAPI.v2.basics.ModCollectionViewer;
import modulesAPI.v2.basics.ModTextField;
import com.caterina.components.CPanel;
import com.caterina.resources.CModules;
import com.caterina.resources.CWords;

import event.EventState;
import event.PrePreparationState;

public class MEventState extends Module<EventState>{
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione dello stato evento
	 * */
	public MEventState(Module<?> ownerModule, EventState content, String tag,String description, ViewType viewType,int... options) {
		super(ownerModule, content, tag, description, viewType,options);
		String name = this.getContent().getName();
		this.stateName=new ModTextField(this, CWords.getWord(name), "EVENT_STATE_NAME", "EVENT_STATE_NAME_DESC", this.getViewType());
		if(name.equalsIgnoreCase("PRE_PREPARATION_STATE")){
			PrePreparationState s = (PrePreparationState)this.getContent();
			this.partecipantMissingGazebo=new ModTextField(this, Integer.toString(s.getPartecipantMissingGazebo()),"PARTECIPANT_MISSING_GAZEBO","PARTECIPANT_MISSING_GAZEBO_DESC", this.getViewType());
			this.partecipantMissingTable=new ModTextField(this, Integer.toString(s.getPartecipantMissingTable()), "PARTECIPANT_MISSING_TABLE", "PARTECIPANT_MISSING_TABLE_DESC", this.getViewType());
			ArrayList<Module<?>> res = new ArrayList<Module<?>>();
			for(Resource r: s.getReservedResources())
				res.add(new MResource(this, r,"RESERVED_RESOURCE","RESERVED_RESOURCE_DESC", this.getViewType()));
			reservedResources = new ModCollectionViewer(this, res,"RESERVED_RESOURCES", "RESERVED_RESOURCES_DESC", this.getViewType());
			reservedResources.addModuleChangedListener(this);
			this.addModules(stateName, partecipantMissingTable, partecipantMissingGazebo, reservedResources);
		}else
			this.addModuleChangedListener(stateName);
		}
		@Override
		protected CPanel getReadOnlyPanel(boolean isSelected) {
			return this.getDefaultPanel();
		}
		@Override
		protected CPanel getWritablePanel(boolean isSelected) {
			return this.getDefaultPanel();
		}
		@Override
		public CPanel getInListPanel(boolean isSelected) {
			return CModules.formatForList(this.getContent().getName(), "", isSelected);
		}
		protected void beforeFireChange(){
			if(this.getContent().getName().equalsIgnoreCase("PRE_PREPARATION_STATE")){
				ArrayList<Resource> r = new ArrayList<Resource>();
				for(Module<?> m : reservedResources.getContent())
					r.add((Resource)m.getContent());
				this.setContent(new PrePreparationState(
						this.getContent().getId(), 
						Integer.valueOf(partecipantMissingTable.getContent()),
						Integer.valueOf(partecipantMissingGazebo.getContent()),
						r)
				);
			}
		}
		@Override
		protected void beforeFireChange(Module<?> mod) {
		}
	
	
		@Override
		protected void afterFireChange(Module<?> mod) {		
		}
		@Override
		protected CPanel getOneLinePanel(boolean isSelected) {
			return null;
		}
		private Module<String> partecipantMissingTable;
		private Module<String> partecipantMissingGazebo;
		private Module<String> stateName;
		private ModCollectionViewer reservedResources;
}
